define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function (params) {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: (params?params.index_url:'facrm/contract/plan/index')+ location.search,
                    add_url: 'facrm/contract/plan/add',
                    edit_url: 'facrm/contract/plan/edit',
                    del_url: 'facrm/contract/plan/del',
                    multi_url: 'facrm/contract/plan/multi',
                    import_url: 'facrm/contract/plan/import',
                    table: 'facrm_contract_plan',
                }
            });
            var table = $("#table");
            var plan_time = Backend.api.query('plan_time');
			var statusstr = Backend.api.query('status');
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                $("input[name='plan_time']").val(plan_time?plan_time:'');

            });
            var columnss = [
                {checkbox: true},
                {field: 'id', title: __('Id'), sortable: true,},
                {
                    field: 'customer_id', title: __('客户'), formatter: function (value, row, index) {
                        if (!row.customer) return '-';
                        return '<a href="javascript:void(0);" data-url="facrm/customer/index/detail/ids/' + row.customer.id
                            + '" data-area="[&quot;98%&quot;,&quot;98%&quot;]" class="btn-dialog" data-title="' + row.customer.name + '">' + row.customer.name + '(' + row.customer_id + ')' + '</a>';
                    }
                },


                {
                    field: 'contract_id', title: __('合同编号'), align: 'left', formatter: function (value, row, index) {
                        if (!row.contract) return '-';
                        return '<a href="javascript:void(0);" data-url="facrm/contract/index/detail/ids/' + row.contract_id
                            + '" data-area="[&quot;98%&quot;,&quot;98%&quot;]" class="btn-dialog" data-title="合同详情:' + row.contract.number + '">' + row.contract.number + '</a>';
                    },
                    addclass: 'selectpage',
                    extend: 'data-source="facrm/contract/index/selectpage" data-field="name"'
                },
                {
                    field: 'name',
                    title: __('计划名称'),
                    operate: 'LIKE',
                    table: table,
                    class: 'autocontent',
                    formatter: Table.api.formatter.content
                },

                {field: 'money', title: __('Money'), operate: 'BETWEEN', sortable: true,},
                {
                    field: 'status',
                    title: __('状态'),
                    sortable: true,
                    formatter: Table.api.formatter.status,
                    searchList: Config.status_list, operate: 'in', addClass:'selectpicker', data:"multiple",
                },

                {
                    field: 'remarks',
                    title: __('Remarks'),
                    operate: 'LIKE',
                    table: table,
                    class: 'autocontent',
                    formatter: Table.api.formatter.content
                },
                {
                    field: 'plan_time',
                    title: __('Plan_time'),
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    autocomplete: false,
                    datetimeFormat: "YYYY-MM-DD",
                    formatter: Table.api.formatter.datetime,
                    sortable: true,
                },
                {
                    field: 'create_time',
                    title: __('Create_time'),
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    autocomplete: false,
                    formatter: Table.api.formatter.datetime, sortable: true,
                },
                {
                    field: 'update_time',
                    title: __('Update_time'),
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    autocomplete: false,
                    formatter: Table.api.formatter.datetime
                },
                {
                    field: 'account_id',
                    title: __('收款账户'),
                    formatter: Table.api.formatter.status,
                    searchList: Config.account_list
                },
                {
                    field: 'admin_id',
                    title: __('Admin_id'),
                    addclass: "selectpage",
                    formatter: function (v, r) {
                        if (!r.admin) return '-';
                        return r.admin.nickname;
                    },
                    extend: 'data-source="facrm/common/selectpage/model/admin?type=all" data-field="nickname" data-orderBy="id desc"'
                },


            ];
            if (params && params.hasOwnProperty('select')) {
                //如果是选择
                columnss.push({
                    field: 'operate', title: __('Operate'), events: {
                        'click .btn-chooseone': function (e, value, row, index) {
                            Fast.api.close(row);
                        },
                    },
                    formatter: function () {
                        return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                    }
                });

            } else {
                columnss.push({
                    field: 'operate',
                    title: __('Operate'),
                    table: table,
                    events: Table.api.events.operate,
                    formatter: Table.api.formatter.operate,
                    buttons: [
                        {
                            name: 'complete',
                            text: '回款',
                            title: __('回款'),
                            icon: 'fa fa-check-circle',
                            classname: 'btn btn-xs btn-info btn-dialog',
                            url:function(row){

                                return  'facrm/contract/receivables/add/ids/'+row.contract_id+"?plan_id="+row.id;
                            },
                            hidden:function (row) {
                                if (row.status==2) return true;
                            },
                            success:function () {
                                $(".btn-refresh").trigger('click');
                            }
                        },

                    ]});
            }



            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    columnss
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            Controller.api.selectcontracts();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        select: function () {
            Controller.index({
                select: true,
                index_url: 'facrm/contract/plan/select'
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            selectcontracts: function () {
                $("#c-customer_id").bind("change", function () {
                    $("#select-resources").html("点击请选择");
                    $("input[name='row[c-contract_id]']").val(0);
                });
                var refreshkey = function (data) {
                    console.log(data);
                    var contracts_id = data.id;
                    $("input[name='row[contract_id]']").val(data.id).trigger("change");
                    $("#select-resources").html(data.name);
                    $('#c-customer_id').val(data.customer_id);
                    $('#c-customer_id').selectPageRefresh();
                };
                $(document).on('click', "#select-resources", function () {
                    var customer_id = $("input[name='row[customer_id]']").val();
                    parent.Backend.api.open($(this).data("url") + "?customer_id=" + customer_id, __('Select'), {callback: refreshkey});
                    return false;
                });
            },

        }
    };
    return Controller;
});
